package cn.xcyy.itcc.service.impl;

import cn.xcyy.itcc.domain.Course;
import cn.xcyy.itcc.domain.KillActivity;
import cn.xcyy.itcc.domain.KillCourse;
import cn.xcyy.itcc.enums.GlodalErrorCode;
import cn.xcyy.itcc.fegin.CourseFeignClient;
import cn.xcyy.itcc.mapper.KillCourseMapper;
import cn.xcyy.itcc.result.JSONResult;
import cn.xcyy.itcc.service.IKillActivityService;
import cn.xcyy.itcc.service.IKillCourseService;
import cn.xcyy.itcc.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-10-20
 */
@Service
public class KillCourseServiceImpl extends ServiceImpl<KillCourseMapper, KillCourse> implements IKillCourseService {


    @Autowired
    private CourseFeignClient courseFeignClient;

    @Autowired
    private IKillActivityService killActivityService;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    /*
     * @Description: 新增秒杀课程
     * @param killCourse: 秒杀课程信息
     **/
    @Override
    public boolean insert(KillCourse killCourse) {
        // 1.调用课程服务，判断课程是否存在，是否是上架状态
        JSONResult jsonResult = courseFeignClient.get(killCourse.getCourseId());
        Course course = JSONObject.parseObject(JSON.toJSONString(jsonResult.getData()), Course.class);
        AssertUtil.isNotNull(course, GlodalErrorCode.UNKNOW_EXCEPTION);
        boolean isStatus = course.getStatus() == Course.STATUS_ON;
        AssertUtil.isTrue(isStatus, GlodalErrorCode.UNKNOW_EXCEPTION);
        // 2.判断此课程是否已经加入此秒杀活动
        KillCourse killCourseTmp = selectKillCourseByCourseIdAndActivityId(killCourse.getActivityId(), killCourse.getCourseId());
        AssertUtil.isNull(killCourseTmp, GlodalErrorCode.UNKNOW_EXCEPTION);
        // 3.活动状态必须是待发布才可以加入活动
        KillActivity killActivity = killActivityService.selectById(killCourse.getActivityId());
        AssertUtil.isNotNull(killActivity, GlodalErrorCode.UNKNOW_EXCEPTION);
        boolean isPublishStatus = KillActivity.WAIT_PUBLISHSTATUS == killActivity.getPublishStatus();
        AssertUtil.isTrue(isPublishStatus, GlodalErrorCode.UNKNOW_EXCEPTION);
        // 4.设置前端未传递的值
        killCourse.setCreateTime(new Date());
        killCourse.setTimeStr(killActivity.getTimeStr());
        killCourse.setTeacherNames(course.getTeacherNames());
        killCourse.setEndTime(killActivity.getEndTime());
        killCourse.setStartTime(killActivity.getBeginTime());
        killCourse.setPublishStatus(killActivity.getPublishStatus());
        return super.insert(killCourse);
    }


    /*
     * 根据秒杀活动ID+课程ID，查询秒杀课程
     **/
    private KillCourse selectKillCourseByCourseIdAndActivityId(Long activityId, Long courseId) {
        Wrapper<KillCourse> wrapper = new EntityWrapper<>();
        wrapper.eq("activity_id", activityId);
        wrapper.eq("course_id", courseId);
        return selectOne(wrapper);
    }

    @Override
    public List<KillCourse> onlineAll() {
        // 1.通过前缀匹配的方式获取到所有加入到Redis中的秒杀商品key值
        Set<Object> keys = redisTemplate.keys("killactivity:*");
        // 2.根据key获取到所有value
        List<KillCourse> killCourses = new ArrayList<>();
        keys.forEach(key->{
            List values = redisTemplate.opsForHash().values(key);
            killCourses.addAll(values);
        });
        // 3.返回所有秒杀课程
        return killCourses;
    }

}
